package com.xu.book.servlet;

import com.xu.book.bean.Book;
import com.xu.book.bean.BorrowCard;
import com.xu.book.bean.BorrowRecoder;
import com.xu.book.service.IBookService;
import com.xu.book.service.IBorrowCardService;
import com.xu.book.service.IBorrowRecoderService;
import com.xu.book.service.impl.BookServiceimpl;
import com.xu.book.service.impl.BorrowCardServiceimpl;
import com.xu.book.service.impl.BorrowRecoderServiceimpl;
import com.xu.sys.bean.SysUser;
import com.xu.sys.servlet.BaseServlet;
import com.xu.sys.utils.Constant;
import com.xu.sys.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "borrowRecoderServlet",urlPatterns = {"/book/borrowRecoderServlet"})
public class BorrowRecoderServlet extends BaseServlet {

    private IBorrowRecoderService service = new BorrowRecoderServiceimpl();
    private IBorrowCardService cardService =new BorrowCardServiceimpl();
    private IBookService bookService = new BookServiceimpl();
    @Override
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        super.list(req, resp);
        service.listPage(pageUtils,getCurrentLoginUser(req,resp));
        req.setAttribute(Constant.LIST_PAGE_UTILS,pageUtils);
        req.getRequestDispatcher("/book/bookRecoder/list.jsp").forward(req,resp);
    }

    @Override
    public void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String bookId = req.getParameter("bookId");
        String cardId = req.getParameter("cardId");
        String id = req.getParameter(Constant.PARAMETER_ID);
        SysUser currentLoginUser = this.getCurrentLoginUser(req, resp);
        BorrowRecoder entity = new BorrowRecoder();
        if (StringUtils.isNotEmpty(cardId)){
            //借书卡不为空
            BorrowCard card = cardService.findById(Integer.parseInt(cardId));
            entity.setCardid(card.getId());
            entity.setStuname(card.getStuname());
            entity.setUserid(card.getUserID());
            if (StringUtils.isEmpty(id)){
                //是添加借书记录同步更新图书的状态
                card.setState(1);
                cardService.updateById(card);
            }
        }
        if (StringUtils.isNotEmpty(bookId)){
            Book book = bookService.findById(Integer.parseInt(bookId));
            entity.setBookid(book.getId());
            entity.setBookname(book.getName());
            if (StringUtils.isEmpty(id)){
                //更新图书的状态
                book.setState(1);
                bookService.updateById(book);
            }
        }
        if (StringUtils.isNotEmpty(id)){

        }else{
            //同步更新书籍和借书卡的状态
            service.save(entity);
        }
        PrintWriter writer = resp.getWriter();
        writer.write("ok");
        writer.flush();

    }

    @Override
    public void saveOrUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws Exception {



    }

    public void backBook(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //根据id查询出对应的借阅信息
        String id = req.getParameter(Constant.PARAMETER_ID);
        BorrowRecoder entity = service.findById(Integer.parseInt(id));
        entity.setEndtime(new Date());
        service.updateById(entity);
        //需要同时更新 书籍和借书卡信息
        Book book = bookService.findById(entity.getBookid());
        book.setState(0);
        bookService.updateById(book);

        BorrowCard card = cardService.findById(entity.getCardid());
        card.setState(0);
        cardService.updateById(card);
        PrintWriter writer = resp.getWriter();
        writer.write("ok");
        writer.flush();
    }
    @Override
    public void remove(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }

    @Override
    public void findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {

    }
}
